package com.adcc.automation.gui.Controller;


import io.datafx.controller.ViewController;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@ViewController(value = "/fxml/update.fxml", title = "更新日志")
public class UpdateController {
    private static final Logger logger = LoggerFactory.getLogger(AppController.class);
    @FXMLViewFlowContext
    private ViewFlowContext context;
    @FXML
    private TextFlow textFlow; // 用于显示文件内容
    
    @PostConstruct
    public void initUI() throws Exception {
        Objects.requireNonNull(context);
        //注册控制类到上下文
        context.register("update", this);
        // 读取 update.txt 文件
        loadUpdateText();
    }
    
    private void loadUpdateText() {
        StringBuilder content = new StringBuilder();
        try (InputStream inputStream = getClass().getResourceAsStream("/update.txt")) {
            if (inputStream == null) {
                throw new FileNotFoundException("Resource /update.txt not found.");
            }
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    content.append(line).append("\n");
                }
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        
        // 清空 TextFlow 准备新的内容
        textFlow.getChildren().clear();
        
        animateText(content.toString());
    }
    
    public void animateText(String content) {
        // 清空 TextFlow 准备新的内容
        textFlow.getChildren().clear();
        
        String[] lines = content.split("\n");
        int[] currentLine = {0}; // 用于跟踪当前行
        int[] currentCharIndex = {0}; // 用于跟踪当前字符索引
        
        // 使用 Timeline 来定时更新内容
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(new KeyFrame(Duration.millis(50), event -> {
            if (currentLine[0] < lines.length) {
                String line = lines[currentLine[0]];
                
                // 如果当前字符索引小于行的长度，则添加字符
                if (currentCharIndex[0] < line.length()) {
                    Text text = new Text(String.valueOf(line.charAt(currentCharIndex[0])));
                    text.setStyle("-fx-font-size: 16px;-fx-font-weight: bold;-fx-font-family: '微软雅黑'");
                    text.setFill(Color.WHITE);
                    textFlow.getChildren().add(text);
                    currentCharIndex[0]++; // 移动到下一个字符
                } else {
                    // 如果行输出完成，添加换行并准备下一行
                    textFlow.getChildren().add(new Text("\n")); // 添加换行
                    currentLine[0]++;
                    currentCharIndex[0] = 0; // 重置字符索引
                }
            } else {
                timeline.stop();
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play(); // 启动动画
    }
}
